package com.huatai.datacenter.controller.datadesensitization;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.datacenter.entity.datadesensitization.DesensitizationRuleEntity;
import com.huatai.datacenter.entity.dto.EncryDto;
import com.huatai.datacenter.service.DesensitizationRuleService;
import com.huatai.datacenter.utils.KeyGeneratorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author gusiyi
 * @version 1.0
 * @className MaskRuleController
 * @description 描述：
 * @date 2023/6/12 15:43
 */
@RestController
@Api(value = "脱敏脱密规则管理",tags = "脱敏脱密规则管理")
@RequestMapping("/desensitization/rule")
public class DesensitizationRuleController {

	@Autowired
	private DesensitizationRuleService desensitizationRuleService;


	@RequestMapping("/getList")
	@ApiOperation("获取脱敏脱密规则清册")
	@ApiOperationSupport(order = 1,includeParameters = {
		"desensitizationRuleEntity.desensitizationName",
		"desensitizationRuleEntity.pageIndex",
		"desensitizationRuleEntity.pageSize"
	})
	public R<Page<DesensitizationRuleEntity>> getDesensitizationRuleList(@RequestBody DesensitizationRuleEntity desensitizationRuleEntity){
		Page<DesensitizationRuleEntity> rulePage = desensitizationRuleService.getRuleList(desensitizationRuleEntity);
		return R.data(rulePage,"获取规则清册成功");
	}

	@RequestMapping("/getRuleListByDataModelId")
	@ApiOperation("通过数据模型获取脱敏脱密规则")
	@ApiOperationSupport(order = 7,includeParameters = {
		"desensitizationRuleEntity.dataModelId",
		"desensitizationRuleEntity.desensitizationObj",
	})
	public R<Map<String,Object>> getRuleListByDataModelIdAndFieldName(@RequestBody DesensitizationRuleEntity desensitizationRuleEntity){
		return R.data(desensitizationRuleService.getRuleListByDataModelIdAndFieldName(desensitizationRuleEntity));
	}

	@RequestMapping("/saveOne")
	@ApiOperation("创建脱敏脱密规则")
	@ApiOperationSupport(order = 2,includeParameters = {
		"desensitizationRuleEntity.desensitizationName",
		"desensitizationRuleEntity.dataModelId",
		"desensitizationRuleEntity.dataModelName",
		"desensitizationRuleEntity.desensitizationFieldName",
		"desensitizationRuleEntity.desensitizationTypeNum",
		"desensitizationRuleEntity.desensitizationTypeName",
		"desensitizationRuleEntity.desensitizationRuleTypeNum",
		"desensitizationRuleEntity.desensitizationRuleTypeName"
	})
	public R<DesensitizationRuleEntity> saveDesensitizationRule(@RequestBody DesensitizationRuleEntity desensitizationRuleEntity){
		return R.status(desensitizationRuleService.save(desensitizationRuleEntity));
	}

	@RequestMapping("/updateById")
	@ApiOperation("修改脱敏脱密规则")
	@ApiOperationSupport(order = 3,includeParameters = {
		"desensitizationRuleEntity.id",
		"desensitizationRuleEntity.desensitizationName",
		"desensitizationRuleEntity.dataModelId",
		"desensitizationRuleEntity.dataModelName",
		"desensitizationRuleEntity.desensitizationFieldName",
		"desensitizationRuleEntity.desensitizationTypeNum",
		"desensitizationRuleEntity.desensitizationTypeName",
		"desensitizationRuleEntity.desensitizationRuleTypeNum",
		"desensitizationRuleEntity.desensitizationRuleTypeName"
	})
	public R<DesensitizationRuleEntity> updateById(@RequestBody DesensitizationRuleEntity desensitizationRuleEntity){
		desensitizationRuleEntity.setUpdateTime(null);
		desensitizationRuleEntity.setUpdateUserid(null);
		desensitizationRuleEntity.setDeleted(null);
		return R.status(desensitizationRuleService.updateById(desensitizationRuleEntity));
	}

	@RequestMapping("/deleteByIds")
	@ApiOperation("删除脱敏脱密规则")
	@ApiOperationSupport(order = 4,includeParameters = {
		"desensitizationRuleEntity.id"
	})
	public R<DesensitizationRuleEntity> updateById(@RequestBody List<DesensitizationRuleEntity> desensitizationRuleEntity){
		return R.status(desensitizationRuleService.deleteByIds(desensitizationRuleEntity));
	}

	@RequestMapping("/desensitization")
	@ApiOperation("数据脱敏")
	@ApiOperationSupport(order = 5,includeParameters = {

	})
	public R<Map<String,Object>> desensitization(@RequestBody JSONObject jsonObject){

		JSONArray desenIds = jsonObject.getJSONArray("desenIds");
		Long[] ids = new Long[desenIds.size()];
		Long[] longs = desenIds.toArray(ids);
		return R.data(desensitizationRuleService.desensitizationData(jsonObject.getJSONObject("key"),longs,jsonObject.getString("data")));
	}

	@RequestMapping("/getKeys")
	@ApiOperation("生成密钥测试")
	@ApiOperationSupport(order =6)
	public String getKeys() throws Exception {
		return KeyGeneratorUtils.generateAllKey();
	}


//	@RequestMapping("/desensitizations")
//	@ApiOperation("数据脱敏")
//	@ApiOperationSupport(order = 5,includeParameters = {
//
//	})
//	public R<Map<String,Object>> desensitizations(){
//		JSONArray desenIds = new JSONArray();
//		desenIds.add(1791034434950344705L);
//		JSONObject key = new JSONObject();
//		String data = "[{\"phone\":\"18586421585\"}]";
//		JSONObject jsonObject = new JSONObject();
//		jsonObject.put("desenIds",desenIds);
//		jsonObject.put("key",key);
//		jsonObject.put("data",data);
//
//
//		JSONArray desenId = jsonObject.getJSONArray("desenIds");
//		Long[] ids = new Long[desenId.size()];
//		Long[] longs = desenId.toArray(ids);
//		return R.data(desensitizationRuleService.desensitizationDatas(jsonObject.getJSONObject("key"),longs,jsonObject.getString("data")));
//	}

	@RequestMapping("/desensitizations")
	@ApiOperation("数据脱敏")
	@ApiOperationSupport(order = 5,includeParameters = {

	})
	public R<EncryDto> desensitization(){
		// 创建 desenIds 数组
		JSONArray desenIds = new JSONArray();
		desenIds.add(1791034434950344705L); // 注意使用 Long 类型避免精度丢失
		// 创建 key 空对象
		JSONObject key = new JSONObject();
		// 创建 data 字符串（注意转义引号）
		String data = "[{\"phone\":\"18586421585\"}]";
		// 构建主 JSON 对象
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("desenIds", desenIds);
		jsonObject.put("key", key);
		jsonObject.put("data", data); // 注意这里保持为字符串类型

		JSONArray desenId = jsonObject.getJSONArray("desenIds");
		Long[] ids = new Long[desenId.size()];
		Long[] longs = desenId.toArray(ids);
		Long requrtId = null;
		return R.data(desensitizationRuleService.desensitizationDataCopy(jsonObject.getJSONObject("key"),longs,jsonObject.getString("data"),requrtId));
	}


}
